package com.example.api.project.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;




public class Unixtime {
	 /**
     * 指定时间戳转换成日期格式字符串
     *
     * @param UnixTime 时间戳
     * @param format   如：yyyy-MM-dd HH:mm:ss
	 * @return 
     *
     * @return
     */
	public  String changge(String UnixTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
	try {
		if (UnixTime==null||UnixTime.equals("")) {
		}else {
			Long timestamp = Long.parseLong(UnixTime)*1000;
			return sdf.format(timestamp);
		}
		
	} catch (Exception e) {
		// TODO: handle exception
	}
	return "";
		

	}
	 /**
     * 指定时间戳转换成日期格式字符串
     *
     * @param UnixTime 时间戳 
	 * @return 
     *
     * @return
     */
	public  String changeToHM(String UnixTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		
	try {
		if (UnixTime==null||UnixTime.equals("")) {
		}else {
			Long timestamp = Long.parseLong(UnixTime)*1000;
			return sdf.format(timestamp);
		}
		
	} catch (Exception e) {
		// TODO: handle exception
	}
	return "";
		

	}
	 /**
     * 指定时间戳转换成日期格式字符串
     *
     * @param UnixTime 时间戳
     * @param format   如：yyyy-MM-dd HH:mm:ss
	 * @return 
     *
     * @return
     */
	public  String days_to_ship(String oldtime,int i) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    try
    {
      Date d1=new Date();
      Date d2 = df.parse(oldtime);
      long diff = (1000 * 60 * 60 * 24*i)-(d1.getTime() - d2.getTime());//这样得到的差值是微秒级别
      long days = diff / (1000 * 60 * 60 * 24);
 
      long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
      long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
     
    /*  if (minutes<0) {
    	  return "0天";
	}*/
      return ""+days+"天"+hours+"小时"+minutes+"分";
    }catch (Exception e){
    	
    }
	return "时间数据不全";
   
	}
	
	
	public static void main(String[] args) throws ParseException {
		
		Unixtime ux=new Unixtime();
		System.out.println(ux.getOldMinTimeStamp("1559018400",5));
		 
	}
	 /**
     * 指定时间戳和当前时间比较
     *
     * @param UnixTime 时间戳
     * @param format   如：yyyy-MM-dd HH:mm:ss
	 * @return 
     *
     * @return
     */
	public  boolean KDtime(String oldtime,int i) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try
    {
      Date d1=new Date();
      Date d2 = df.parse(oldtime);
      long diff = (1000 * 60 * 60 *i)-(d1.getTime() - d2.getTime());//这样得到的差值是微秒级别
     
      if (diff<0) {
    	  return true;
	}
      return false;
    }catch (Exception e){
    	//请输入正确参数
    }
	return false;
   
	}


	 /**
     * 指定日期/格式,字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public  String Date2TimeStamp(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return nowTimeStamp
     */
    public  String getNowTimeStamp() {
        long time = System.currentTimeMillis();
        String nowTimeStamp = String.valueOf(time / 1000);
        return nowTimeStamp;
    }
    /**
     * 取得当前时间,几天前时间戳（精确到秒）
     *
     * @return nowTimeStamp
     */
    public  String getOldTimeStamp(int day) {
        long time = System.currentTimeMillis();
        time=time-1000*24*60*60*day;
        String oldTimeStamp = String.valueOf(time / 1000);
        return oldTimeStamp;
    }
    
    /**
     * 取得指定时间,几天之后时间戳（精确到秒）
     *
     * @return nowTimeStamp
     */
    public  String getOldTimeStamp(String days,int day) {
        long time = System.currentTimeMillis();
        time=time-1000*24*60*60*day;
        String oldTimeStamp = String.valueOf(time / 1000);
        
        return oldTimeStamp;
    }
    
    /**
     * 取得指定时间戳,几分钟之前时间戳 
     *
     * @return oldTimeStamp
     * @throws ParseException 
     */
    public  String getOldMinTimeStamp(String TimeStamp,int min) throws ParseException {
//    	SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    	   Date date = format.parse(TimeStamp); 
    	   //转换为Date类
    	   Long timestamp=Long.parseLong(TimeStamp);
    	   timestamp=timestamp- 60*min;
           String oldTimeStamp = String.valueOf(timestamp  ); 
           return oldTimeStamp;
    }
 
    
    
    /**
     * 取得以前时间戳（精确到秒）
     *
     * @return nowTimeStamp
     */
    public  String getMinOldTimeStamp(int min) {
        long time = System.currentTimeMillis();
        time=time-1000*60*min;
        String oldTimeStamp = String.valueOf(time / 1000);
        return oldTimeStamp;
    }
    
    /** 
    * 获得指定日期的后几天,时间为空则返回当前时间后几天 
    * @param specifiedDay 
    * @return 
    */ 
    public  String getSpecifiedDayAfter(String specifiedDay,int i){ 
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	if (specifiedDay==null) {//判断是否为空
    		specifiedDay= sdf.format(new Date());
		}
    Calendar c = Calendar.getInstance(); 
    Date date=null; 
    try { 
    date = sdf.parse(specifiedDay); 
    } catch (ParseException e) { 
    e.printStackTrace(); 
    } 
    c.setTime(date); 
    int day=c.get(Calendar.DATE); 
    c.set(Calendar.DATE,day+i);  
    String dayAfter=sdf.format(c.getTime()); 
    return dayAfter; 
    } 

}
